/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import javax.mail.PasswordAuthentication;
import java.util.Properties;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import service.MumuUser;
import service.MumuUserFacadeREST;
import utilities.ReadSmtp;

/**
 *
 * @author andrew
 */
@Stateless
public class EmailSender {

    private Session session;
    
     @EJB
     private MumuUserFacadeREST userManager;
    
    
    public void requestAccess(String username, String email, String password, String qualification, String grantUrl, HttpServletRequest request){
       
       // inserimento iferimento in db senza essere validato 
       MumuUser  newUser = new MumuUser();
       newUser.setNameSurname(username);
       newUser.setMail(email);
       newUser.setPassword(password);
       newUser.setQualification(qualification);
       newUser.setIsAdministrator(false);
       newUser.setIsValidated(false);
       newUser.setBundleID("");
       
       userManager.edit(newUser);
       
       final ReadSmtp readSmtp = new ReadSmtp(request);
       
       // invio notifica richiesta tramite mail
       Properties props = new Properties();
       props.put("mail.smtp.host", readSmtp.getSmtp_address());
       props.put("mail.smtp.socketFactory.port", readSmtp.getSmtp_port());
       props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
       props.put("mail.smtp.auth", readSmtp.getAuth());
       props.put("mail.smtp.port", readSmtp.getSmtp_port());
       
       session = Session.getInstance(props,
               new javax.mail.Authenticator() {
                   @Override
                   protected PasswordAuthentication getPasswordAuthentication(){
                       // email address e password
                       return new PasswordAuthentication(readSmtp.getAuth_username(), readSmtp.getAuth_password());
                   }
               });
     
       try{
           
           grantUrl = grantUrl.replace("localhost:8181", "mumu-ibc.it");
           System.out.println("VALORE GRANT: "+grantUrl);
           Message message = new MimeMessage(session);
           message.setFrom(new InternetAddress(readSmtp.getAuth_username()));
           message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(readSmtp.getAuth_username()));
           message.setSubject("UniverseCreator: Richiesta di Accesso");
           String content = "Gentile Amministratore, <br/><br/>"
                   + "Il seguente utente desidera l'accesso a UniverseCreator. <br/>"
                   + "<br/>"
                   + "Le credenziali inserite dall'utente sono: <br/>"
                   + "<br/>"
                   + "Nome e Cognome inserito: " + username + "<br/>"
                   + "Username proposto: " + email + "<br/>"
                   + "Qualifica dichiarata: " + qualification + "<br/>"
                   + "<br/><br/>"
                   + "<br/><br/>"
                   + "Cliccare uno dei link sottostanti in base alla decisione presa."
                   + "<br/><br/>"
                   + "<a href='"+ grantUrl + "&role=admin'> Consenti l'iscrizione come amministratore</a><br/>"
                   + "<br/><br/>"
                   + "<a href='"+ grantUrl + "&role=guest'> Consenti l'iscrizione come creatore</a><br/>"
                   + "<br/><br/>"
                   + "<a href='"+ grantUrl + "&role=ignore'> Rifiuta la richiesta di registrazione</a><br/>";
           message.setContent(content, "text/html");
           Transport.send(message);
       }catch(Exception e){
           System.err.println(e.getCause());
       }
    }

}
